package com.cd.university.controller;

import com.cd.university.common.RestResult;
import com.cd.university.controller.form.BigProjectFileForm;
import com.cd.university.controller.form.UploadFileDto;
import com.cd.university.service.ProjectFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author 陈栋
 * @create 2021/10/16 14:27
 */
@RestController
@RequestMapping("/projectFile")
@Api("项目文件接口")
public class ProjectFileController {

    @Autowired
    private ProjectFileService projectFileService;

    @PostMapping("/updateLogo")
    @ApiOperation("上传项目的logo")
    public RestResult updateProjectLogo(@RequestParam("logo") MultipartFile logo,
                                        @RequestParam("projectId") Integer projectId) {
        return projectFileService.updateProjectLogo(projectId, logo);
    }

    @GetMapping("/getLogo")
    @ApiOperation("获取项目的logo")
    public RestResult downloadProjectLogo(HttpServletResponse response, @RequestParam("projectId") Integer projectId) {
        return projectFileService.downloadProjectLogo(response, projectId);
    }

    @GetMapping("/searchProjectFile")
    @ApiOperation("查询项目文件")
    public RestResult searchProjectFile(@RequestParam("projectId") Integer projectId,
                                        @RequestParam("fileId") String fileId) {
        return projectFileService.searchProjectFile(projectId, fileId);
    }

    @PostMapping("/updateFile")
    @ApiOperation("批量上传项目的文件")
    public RestResult updateProjectFile(@RequestParam("file") MultipartFile[] files,
                                        @RequestParam("projectId") Integer projectId,
                                        @RequestParam(value = "atId",required = true,defaultValue = "0") Integer atId,
                                        @RequestParam(value = "categoryId",required = true,defaultValue = "0") Integer categoryId,
                                        @RequestParam(value = "status",required = true,defaultValue = "0") Integer status) {
        return projectFileService.updateProjectFile(files, projectId, atId, categoryId, status);
    }

    @GetMapping("/updateBigFile")
    @ApiOperation("上传大文件")
    public RestResult updateBigFile(@Valid @ModelAttribute BigProjectFileForm form) {
        return projectFileService.updateBigFile(form.getIdentifier(),
                form.getProjectId(),
                form.getAtId(),
                form.getCategoryId(),
                form.getStatus(),
                form.getFileName());
    }

    @PostMapping("/updateBigFile")
    @ApiOperation("真正上传大文件")
    public RestResult updateBigFile(HttpServletRequest request, @Valid @ModelAttribute UploadFileDto uploadFileDto) {

        return projectFileService.updateBigFile(request, uploadFileDto);
    }

}
